package com.hgw.easyexceldemo.controller;

import com.alibaba.excel.EasyExcel;
import com.hgw.easyexceldemo.domain.UploadData;
import com.hgw.easyexceldemo.listener.UploadDataListener;
import com.hgw.easyexceldemo.mapper.UploadDataDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * Description:
 * Data time:2022/8/30 15:17
 * Author:TQ-3038-HuangGongWei
 */
@RestController
public class DataImport {

    @Autowired
    private UploadDataDAO uploadDAO;

    /**
     * 文件上传
     * 1. 创建excel对应的实体对象
     * 2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器
     * 3. 直接读即可
     */
    @PostMapping("upload")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), UploadData.class, new UploadDataListener(uploadDAO)).sheet().doRead();
        return "success";
    }
}
